package com.dark.sys.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dark.common.web.controller.BaseController;
import com.dark.common.web.validate.AjaxResponse;
import com.dark.sys.service.ResourceService;
import com.dark.sys.web.dto.ResourceDto;

@Controller
@RequestMapping("/admin/sys/resource")
public class ResourceController extends BaseController {

	@Autowired
	private ResourceService resourceService ;
	
	@RequestMapping(value="/main", method=RequestMethod.GET)
	public String main(Model model) {
		return viewName("main") ;
	}
	
	@RequestMapping(value="/form", method=RequestMethod.GET)
	public String form(Integer id, Integer menuId, Model model) {
		model.addAttribute("menuId", menuId) ;
		if(null != id) {
			model.addAttribute("id", id) ;
		}
		return viewName("form") ;
	}
	
	@ResponseBody
	@RequestMapping(value="/add", method=RequestMethod.POST)
	public AjaxResponse add(ResourceDto dto) {
		this.resourceService.add(dto) ;
		return new AjaxResponse() ;
	}
	
	
	@ResponseBody
	@RequestMapping(value="/delete/{id}", method=RequestMethod.GET)
	public AjaxResponse delete(@PathVariable Integer id) {
		this.resourceService.delete(id) ;
		return new AjaxResponse() ;
	}
	
	@ResponseBody
	@RequestMapping(value="/update", method=RequestMethod.POST)
	public AjaxResponse update(ResourceDto dto) {
		this.resourceService.update(dto) ;
		return new AjaxResponse() ;
	}
	
	@ResponseBody
	@RequestMapping(value="/get/{id}", method=RequestMethod.GET)
	public ResourceDto get(@PathVariable Integer id, Model model) {
		return this.resourceService.get(id) ;
	}
	
	@ResponseBody
	@RequestMapping(value="/tree")
	public List<ResourceDto> tree(ResourceDto dto) {
		return this.resourceService.tree(dto) ;
	}
	
	@ResponseBody
	@RequestMapping(value="/listAll")
	public List<ResourceDto> listAll(ResourceDto dto) {
		return this.resourceService.listAll(dto) ;
	}
	
	@ResponseBody
	@RequestMapping(value="/getUserResourceOperations/{userId}", method=RequestMethod.GET)
	public List<Integer> getUserResourceOperations(@PathVariable Integer userId) {
		return this.resourceService.getResourceOperationsIdsByUserId(userId) ;
	}
	
	
}
